home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-10 | 4.6 KB | 189 lines | [TEXT/CWIE] |
- // TTalker.cp
- // Copyright © 1994 Apple Computer Inc. All rights reserved.
-
- //=======================================================================
- #ifndef _TTALKER_
- #include "TTalker.h"
- #endif
-
- #ifndef _TCOMPUTER_
- #include "TComputer.h"
- #endif
-
- #ifndef __STRINGS__
- #include "Strings.h" // c2pstr, p2cstr
- #endif
-
- #ifndef _STRING
- #include "String.h" // strcpy
- #endif
-
- #ifndef __FP__
- #include <FP.h> // num2dec
- #endif
-
- #ifndef NIL
- #define NIL nil
- #endif
-
- //=======================================================================
- TTalker::TTalker()
- : fSpeechChannel(NIL),
- fComputerHasTextToSpeech(FALSE),
- fNumVoices(0)
- {
- fComputerHasTextToSpeech = TComputer::HasTextToSpeech();
- if (fComputerHasTextToSpeech) {
- OSErr err = ::NewSpeechChannel(NIL, &fSpeechChannel);
- err = ::GetSpeechRate(fSpeechChannel, &fSpeechRate);
- err = ::CountVoices(&fNumVoices);
- }
- }
-
- //----------------------------------------------------------------------
- TTalker::~TTalker()
- {
- OSErr err = ::DisposeSpeechChannel(fSpeechChannel);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::UseVoiceNamed(char* name)
- {
- if (fComputerHasTextToSpeech) {
- StringPtr pascalName = c2pstr(name);
- OSErr err;
- VoiceSpec spec;
- VoiceDescription description;
- for (short index = 0; index <= fNumVoices; index++) {
- err = ::GetIndVoice(index, &spec);
- err = ::GetVoiceDescription(&spec, &description, sizeof(description));
- if ( ::EqualString(description.name, pascalName, FALSE, FALSE) ) {
- this->UseVoice(index);
- } // if strcmp
- } // for
- } // if fComputerHasTextToSpeech
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::UseVoice(short index)
- {
- if (fComputerHasTextToSpeech) {
- OSErr err;
- VoiceSpec spec;
- VoiceDescription description;
- long infoLength = sizeof(description);
- err = ::GetIndVoice(index, &spec);
- err = ::GetVoiceDescription(&spec, &description, infoLength);
- err = ::DisposeSpeechChannel(fSpeechChannel);
- fSpeechChannel = NIL;
- err = ::NewSpeechChannel(&spec, &fSpeechChannel);
- err = ::SetSpeechRate(fSpeechChannel, fSpeechRate);
- } // if fComputerHasTextToSpeech
- }
-
- //----------------------------------------------------------------------
- char*
- TTalker::GetVoiceName(short index)
- {
- OSErr err;
- VoiceSpec spec;
- static VoiceDescription description;
- long infoLength = sizeof(description);
- err = ::GetIndVoice(index, &spec);
- err = ::GetVoiceDescription(&spec, &description, infoLength);
- return p2cstr(description.name);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::SayString(char* str, Boolean wait)
- {
- long bytes = 0;
- while (str[bytes])
- bytes++;
- SayText(str, bytes, wait);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::SayFloat(float number, short digitsDesired, Boolean wait)
- {
- decform format;
- format.style = FIXEDDECIMAL;
- format.digits = digitsDesired;
- decimal numberDecimal;
- ::num2dec(&format, number, &numberDecimal);
- char numberString[40];
- ::dec2str(&format, &numberDecimal, numberString);
- SayString(numberString, wait);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::SayInteger(long number, Boolean wait)
- {
- const short kDigitsDesired = 0;
- this->SayFloat(number, kDigitsDesired, wait);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::SayTextEdit(TEHandle TEHdl, Boolean wait)
- {
- Handle textHdl = (**TEHdl).hText;
- long textBytes = ::GetHandleSize(textHdl);
- ::HLock(textHdl);
- SayText(*textHdl, textBytes, wait);
- ::HUnlock(textHdl);
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::SayText(Ptr textPtr, long textBytes, Boolean wait)
- {
- // all speaking goes through this function
- if (fComputerHasTextToSpeech) {
- OSErr err = ::SpeakText(fSpeechChannel, textPtr, textBytes);
- if (wait)
- WaitUntilDone();
- } // if TComputer
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::WaitUntilDone()
- {
- while (::SpeechBusy() )
- {};
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::Faster()
- {
- if (fComputerHasTextToSpeech) {
- fSpeechRate *= 1.5;
- OSErr err = ::SetSpeechRate(fSpeechChannel, fSpeechRate);
- }
- }
-
- //----------------------------------------------------------------------
- void
- TTalker::Slower()
- {
- if (fComputerHasTextToSpeech) {
- fSpeechRate /= 1.5;
- ::SetSpeechRate(fSpeechChannel, fSpeechRate);
- }
- }
-
- //----------------------------------------------------------------------
- short
- TTalker::GetNumberVoices()
- {
- return fNumVoices;
- }
-
-